function getLine(lineId) {
    currentLineId = (lineId-1) % 4 + 1;
    map.clearOverlays();
    map.removeOverlay(polyline);
    getCarLocation();
    map.centerAndZoom(new BMap.Point(118.102857, 24.577551), 12);  // 初始化地图,设置中心点坐标和地图级别
    //画出站点 1-4出岛,5-8进岛
    if (lineId == 1 || lineId == 5) {//路线为1或5,站点坐标bd9,其他wgs84,需转为bd9
        $.getJSON(root_path + "/findByLine.do", {lineId: lineId}, function (data) {
            // console.log(data.data);
            if (data.status == 0) {
                var points = [];
                var names = [];
                var sites = data.data;
                console.log(sites);
                var site_times = [];
                for (let i = 0; i < sites.length; i++) {
                    names.push(sites[i].siteName);
                    site_times.push(sites[i].costTime);
                    let lng = sites[i].lng;
                    let lat = sites[i].lat;
                    points.push([lng, lat]);
                }
                timesArr = site_times;
                // console.log("points[3]-"+points[3]);
                // console.log("site_times"+timesArr);
                getPointArray(points, names);
                showLineMenu(lineId, points, names);
            }
        });

    } else {
        $.getJSON(root_path + "/findByLine.do", {lineId: lineId}, function (data) {
            // console.log(data.data);
            if (data.status == 0) {
                var points = [];
                var names = [];
                var site_times = [];
                var sites = data.data;
                // if (lineId == 5) {
                //     for (let i = 0; i < sites.length; i++) {
                //         names.push(sites[i].siteName);
                //         site_times.push(sites[i].costTime);
                //         let lng = sites[i].lng;
                //         let lat = sites[i].lat;
                //         points.push([lng, lat]);
                //     }
                // } else {
                    for (let i = 0; i < sites.length; i++) {
                        names.push(sites[i].siteName);
                        site_times.push(sites[i].costTime);
                        var coors = wgs84tobd09(sites[i].lng, sites[i].lat);
                        points.push(coors);
                    }
                // }
                timesArr = site_times;
                // console.log("site_times"+names);
                // console.log("points[3]-"+points[3]);
                // console.log("site_times"+timesArr);
                // if (lineId == 5) {
                //     getPointArray(points, names);
                // } else {
                //     drawPolyLine(getPointArray(points, names));
                // }
                getPointArray(points, names);
                showLineMenu(lineId,points, names);
            }
        });

    }
    //画线
    $.getJSON(root_path + "/findPointByLine.do", {lineId: lineId}, function (data) {
        console.log(data.data.length);
        if (data.status == 0) {
            var sites = data.data;
            var ps = [];
            for (let i = 0; i <= sites.length - 1; i++) {
                var coors = wgs84tobd09(sites[i].lng, sites[i].lat);
                var p = new BMap.Point(coors[0], coors[1]);
                ps.push(p);
            }
            drawPolyLine(ps);
        }
    });

}

function showLineMenu(lineId, points, names) {
    var lineInfo = "";
    var table1 = document.createElement("table");
    for (let i = 0; i < names.length; i++) {
        var predictTime = predictLeftTime(lineId, i+1);
        var hours = parseInt(predictTime / 3600);
        var minutes = parseInt((predictTime % 3600) / 60);
        var seconds = parseInt((predictTime % 3600) % 60);
        if (i == 0) {
            // lineInfo = lineInfo + "<tr><td class='site' >本路线有"+ names.length + "站</td></tr>";
            lineInfo = lineInfo
                + "<li><div class=\"site status-content-before\" id=\"site" + i + "\" >"
                + "起点: " + names[i]
                // + "<a href=\""+root_path+"/getSite.do?lineId=" +lineId +"&index=" +i +"\">去这里</a></div>";
                // + "<a href=\""+root_path+"/map1.html\">去这里</a></div>";
                // + "</div><div class=\"new_html\" id=\""+points[i][0]+","+points[i][1]+","+lineId+","+i+"\" >去这里</div>";
                + "</div><div class=\"new_html\" id=\""+points[i][0]+","+points[i][1]+"\" >去这里</div>";

        } else if (i == names.length - 1) {
            lineInfo = lineInfo + "<li><div class=\"site status-content-before\" id=\"site" + i + "\" >" + "终点: " + names[i]
                // + "<a href=\""+root_path+"/map1.html\">去这里</a></div>";
                + "</div><div class=\"new_html\"  id=\""+points[i][0]+","+points[i][1]+"\" >去这里</div>";
        } else {
            lineInfo = lineInfo + "<li><div class=\"site status-content-before\" id=\"site" + i + "\" >" + i + " " + names[i]
                // + "<a href=\""+root_path+"/map1.html\">去这里</a></div>";
                + "</div><div class=\"new_html\" id=\""+points[i][0]+","+points[i][1]+"\">去这里</div>"
                + "<div><div style=\"color:blue;\">" + "   (约 " +  hours + " 时 " + minutes + " 分 " + seconds + " 秒" + "后到达)" + "</div></div>";
            // 上面这行写到“去这里”后面，时间记得取整
        }
        lineInfo = lineInfo + "<div class=\"status-line\"></div></li>";
    }
    $("#lineInfo").html(lineInfo);
    for (let i = 0; i < points.length; i++) {
        $('#site' + i).on('click', function () {
            move_and_resize(points[i], 17);
            touchReStart();
        });
        // $('#site0').css("font-weight","bold");
        // $('#site'+(points.length-1)).css("font-weight","bold","font-size","22px","margin-bottom","1%");
    }
    $(".new_html").click(function () {
        var coors = $(this).attr('id');
        // coors.split(',');
        console.log(coors);
        var destCoors = coors.split(',');
        window.location = root_path+"/map1.html?destLng="+destCoors[0]+"&destLat="+destCoors[1];
        // var lineId = parseInt(line.substring(4,5));
        // console.log("lineId="+lineId);
        // getLine(lineId);
        // $(".line").css("color", "gray");
        // $(this).css("color", "#4790EF");
    });
}

function showLineMenu1(points, names) {
    var lineInfo = "";
    var table1 = document.createElement("table");
    for (let i = 0; i < names.length; i++) {
        if (i == 0) {
            // lineInfo = lineInfo + "<tr><td class='site' >本路线有"+ names.length + "站</td></tr>";
            lineInfo = lineInfo + "<tr><td class=\"site\" id=\"site" + i + "\">" + "起点: " + names[i] + "</td></tr>";
        } else if (i == names.length - 1) {
            lineInfo = lineInfo + "<tr><td class=\"site\" id=\"site" + i + "\">" + "终点: " + names[i] + "</td></tr>";

        } else {
            lineInfo = lineInfo + "<tr><td class=\"site\" id=\"site" + i + "\">" + i + " " + names[i] + "</td></tr>";
        }
    }
    $("#lineInfo").html(lineInfo);
    for (let i = 0; i < points.length; i++) {
        $('#site' + i).on('click', function () {
            move_and_resize(points[i], 17);
            touchReStart();
        });
        $('#site0').css("font-weight", "bold");
        $('#site' + (points.length - 1)).css("font-weight", "bold", "font-size", "22px", "margin-bottom", "1%");
    }
}

function deleteOverlays() {
    var allOverlay = map.getOverlays();
    for (var i = 0; i < allOverlay.length - 1; i++) {
        // if(allOverlay[i].getLabel().content != "car"){
        map.removeOverlay(allOverlay[i]);
        // return false;
        // }
    }
}
